<?php

class Member_MemberController extends Zend_Controller_Action
{

	private $request;
	private $common;
	private $db;
    public function init()
    {
		$this->request = $this->getRequest();

		include_once "../library/inc/Zyt_Common.php"; 
		$this->common = Zyt_Common::getInstance();
		
		$this->common->checklogin(true, true);

		include_once "../library/inc/Db_Config.php";
		$this->db = Db_Config::getInstance()->Member();
        /* Initialize action controller here */
    }

	function detailAction(){
		$memberid = $this->request->getParam("MemberID");
		$row = $this->db->fetchRow("SELECT m.*, mi.realname, mi.sex, mi.birthday, mi.picture, mi.credit_line, mi.pwd_question, mi.pwd_answer, mi.user_money, mi.coupon_money, mi.points, mi.remark FROM member m LEFT JOIN memberinfo mi ON m.memberid = mi.memberid WHERE m.memberid = :memberid", array(
			"memberid" => $memberid
		));
		
		$dictcat = include_once "../library/inc/DictCat.php";

		$this->common->PutData("{record:[{MemberID:" . $row["memberid"] . ",Email:'" . $row["email"] . "',Mobile:'" . $row["mobile"] . "',UserName:'" . $row["username"] . "', MemberType:'" . $row["membertype"] . "',Rank:'" . $row["rank"] . "',RegsterTime:'" . $this->common->getLocaldate($row['regstertime']) . "',LastLogin:'" . $this->common->getLocaldate($row["lastlogin"]) . "',LastIp:'" . $row["lastip"] . "',LoginTimes:'" . $row["logintimes"] . "',IsOnline:'" . $row["isOnline"] . "', OptionSet:" . $row["optionset"] . " ,Status:'" . $row["status"] . "',LockStatus:'" . $dictcat["status"][$row["status"]] . "', RealName:'" . $row["realname"] . "', Sex: '" . ($row["sex"] == "" ? 2 : $row["sex"]) . "', Birthday: '" . $row["birthday"] . "', Picture: '" . $row["picture"] . "', credit_line: '".(empty($row["credit_line"]) ? 0 : $row["credit_line"])."', Pwd_Question: '" . $row["pwd_question"] . "', Pwd_Answer: '" .$row["pwd_answer"]. "', user_money: '" .$row["user_money"]. "', coupon_money: '".$row["coupon_money"]."', Points: '".$row["points"]."', Remark:'" .$row["remark"]. "'}]}");
		exit;
	}

	public function getlistAction()
	{
		$email = $this->request->getParam("email");
		
		$start = $this->request->getParam("start");
		$limit = $this->request->getParam("limit");
		$where = "WHERE status & 1 = 0 ";
		if(!empty($email)) {
			$where .= " AND (email like '%$email%' Or mobile like '%$email%')";
		}
		$_iAmount = $this->db->fetchOne("SELECT count(*) FROM member $where");
		$result = $this->db->fetchAll("SELECT memberid, email, mobile, username, membertype, rank, regstertime, lastlogin, lastip, logintimes, isOnline, optionset, status FROM member $where limit $start, $limit");
		$sMsg = "";
		$len = count($result);
		for($i=0; $i < $len; $i++) {
			$comma = ",";
			if($i == ($len -1)) 
			{
				$comma = "";
			}
			$sMsg .= "{MemberID:" . $result[$i]["memberid"] . ",Email:'" . $result[$i]["email"] . "',Mobile:'" . $result[$i]["mobile"] . "',UserName:'" . $result[$i]["username"] . "', MemberType:'" . $result[$i]["membertype"] . "',Rank:'" . $result[$i]["rank"] . "',RegsterTime:'" . $result[$i]['regstertime'] . "',LastLogin:'" . $result[$i]["lastlogin"] . "',LastIp:'" . $result[$i]["lastip"] . "',LoginTimes:'" . $result[$i]["logintimes"] . "',IsOnline:'" . $result[$i]["isOnline"] . "', OptionSet:" . $result[$i]["optionset"] . " ,Status:'" . $result[$i]["status"] . "',LockStatus:'" . $result[$i]["status"] . "'}" . $comma;
		}

		$acl = array("aRead" => 1, "aNew" => 1, "aEdit" => 1, "aLock" => 1, "aDel" => 1, "aList" => 1);
		$_sExtJSD = "Acl:[{aRead:" . $acl["aRead"] . ", aNew:" . $acl["aNew"] . ",aEdit:" . $acl["aEdit"] . ",aDel:" . $acl["aDel"] .
							  ",aList:" . $acl["aList"] . "}]";
		$this->common->PutData('{totalProperty:' . $_iAmount . ',records:[' . $sMsg . '],' . $_sExtJSD . '}');
		exit;
	}

	public function saveAction()
	{
			$memberid = $this->request->getParam("MemberID");
			//$email = $this->request->getParam("Email");
			//$mobile = $this->request->getParam("Mobile");
			//$username = $this->request->getParam("UserName");
			$membertype = $this->request->getParam("MemberType");
			$rank = $this->request->getParam("Rank");

			//$realname = $this->request->getParam("RealName");
			//$sex = $this->request->getParam("Sex");
			//$birthday = $this->request->getParam("Birthday");
			//$picture = $this->request->getParam("Picture");
			$credit_line = $this->request->getParam("credit_line");
			//$pwd_question = $this->request->getParam("Pwd_Question");
			//$pwd_answer = $this->request->getParam("Pwd_Answer");
			//$user_money = $this->request->getParam("user_money");
			//$coupon_money = $this->request->getParam("coupon_money");
			//$points = $this->request->getParam("Points");
			$remark = $this->request->getParam("Remark");
			$userid = $this->common->getUserid();

			
		if($memberid > 0) {
			if($this->db->update("member", array(
					//"email" => $email,
					//"mobile"    => $mobile,
					//"password"    => md5($password),
					//"username"	=> $username,
					"membertype" => $membertype,
					"rank"    => $rank
				), $this->db->quoteInto('memberid = ?', $memberid)) >= 0)
				{
					if($this->db->fetchOne("SELECT memberid FROM memberinfo WHERE memberid = :memberid", array("memberid" => $memberid)))
					{
						$this->db->update("memberinfo", array(
							"credit_line" => $credit_line,
							"remark" => $remark
						), $this->db->quoteInto('memberid = ?', $memberid));
					}
					else
					{
						$this->db->insert("memberinfo", array(
							"memberid" => $memberid,
							"realname" => "",
							"sex" => 2,
							"birthday" => 0,
							"picture" => "",
							"credit_line" => $credit_line,
							"pwd_question" => "",
							"pwd_answer"	=> "",
							"user_money"	=> 0,
							"coupon_money"	=> 0,
							"points"		=> 0,
							"remark"		=> $remark
						));
					}
					$this->common->PutData('{result:""}');
				}
				else
				{
					$this->common->PutData('{error:"保存失败!"}');
				}
		}
		else
		{
				$this->common->PutData('{error:"保存失败!"}');
		}

		exit;
	}

	public function spwdAction()
	{
		$password  = $this->request->getParam("LoginPass");
		$password2 = $this->request->getParam("NewLoginPass");
		$memberid   = $this->request->getParam("MemberID");
		if($password != $password2) 
		{
			$this->common->PutData('{error:"两次输入密码不一致，请重新输入!"}');
		}
		else if($this->db->update("member", array(
					"password" => md5($password)
				), $this->db->quoteInto('memberid = ?', $memberid))) 
		{
			$this->common->PutData('{result:""}');
		}
		else
		{
			$this->common->PutData('{error:"更改失败!"}');
		}
		exit;
	}

	public function delAction()
	{
		$uids = $this->request->getParam("aid");
		
		if($this->db->update("member", array(
					"status" => 1
				), $this->db->quoteInto('memberid in (?)', $uids))) 
		{
			$this->common->PutData('{result:""}');
		}
		else
		{
			$this->common->PutData('{error:"操作失败!"}');
		}
		exit;
	}

	public function lockAction()
	{
		$uids = $this->request->getParam("aid");
		
		$stmt = $this->db->query("UPDATE member SET status = 2 - status & 2 WHERE status in (0,2) AND " . $this->db->quoteInto('memberid in (?)', $uids));
		if($stmt->rowCount())
		{
			$this->common->PutData('{result:""}');
		}
		else
		{
			$this->common->PutData('{error:"操作失败!"}');
		}
		exit;
	}
}